﻿namespace SheetLib
{
    using IOLib;
    using System;
    using System.Drawing;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class PictureControl : BaseControl
    {
        private Bitmap BackGroundImage;
        private Graphics GraphicsHandle;
        private bool IsMathControlWasClicked;
        private Point LastMouseCoordinates;
        private Pen SignaturePen;
        private bool sizeChanging;
        private SizeChangeDirection sizeDirection;
        private Point sizeStart;

        public PictureControl()
        {
            this.SignaturePen = new Pen(Color.Black);
            this.LastMouseCoordinates = new Point(0, 0);
            this.sizeStart = new Point(0, 0);
            this.sizeDirection = SizeChangeDirection.Both;
            base.Size = new Size(0xe3, 100);
            this.LoadBackgroundImage();
        }

        public PictureControl(PictureControl value) : base(value)
        {
            this.SignaturePen = new Pen(Color.Black);
            this.LastMouseCoordinates = new Point(0, 0);
            this.sizeStart = new Point(0, 0);
            this.sizeDirection = SizeChangeDirection.Both;
            this.LoadImageBitmap(value.BackGroundImage);
        }

        public void Clear(bool ClearCapturedLines)
        {
            try
            {
                this.LoadBackgroundImage();
                base.RePainter();
                if (!ClearCapturedLines)
                {
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Bitmap ExportImage()
        {
            return this.BackGroundImage;
        }

        public override CursorType GetCursorType(MouseEventArgs e)
        {
            if ((e.X > (base.Size.Width - GlobalParams.ScreenFactor(0x10))) && (e.Y > (base.Size.Height - GlobalParams.ScreenFactor(0x10))))
            {
                return CursorType.SizeNWSE;
            }
            if (((e.X > ((base.Size.Width / 2) - GlobalParams.ScreenFactor(12))) && (e.X < ((base.Size.Width / 2) - GlobalParams.ScreenFactor(4)))) && (e.Y > (base.Size.Height - GlobalParams.ScreenFactor(0x10))))
            {
                return CursorType.SizeV;
            }
            if (((e.X > (base.Size.Width - GlobalParams.ScreenFactor(0x10))) && (e.Y > ((base.Size.Height / 2) - GlobalParams.ScreenFactor(12)))) && (e.Y < ((base.Size.Height / 2) - GlobalParams.ScreenFactor(4))))
            {
                return CursorType.SizeH;
            }
            return base.GetCursorType(e);
        }

        private void LoadBackgroundImage()
        {
            try
            {
                Bitmap backGroundImage = this.BackGroundImage;
                this.BackGroundImage = new Bitmap(base.Size.Width - GlobalParams.ScreenFactor(8), base.Size.Height - GlobalParams.ScreenFactor(8));
                this.GraphicsHandle = Graphics.FromImage(this.BackGroundImage);
                this.GraphicsHandle.Clear(Color.White);
                if (backGroundImage != null)
                {
                    this.GraphicsHandle.DrawImage(backGroundImage, 0, 0);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadBackgroundImageIfInvalid()
        {
            try
            {
                if (((this.BackGroundImage == null) || (this.BackGroundImage.Width != (base.Size.Width - GlobalParams.ScreenFactor(8)))) || (this.BackGroundImage.Height != (base.Size.Height - GlobalParams.ScreenFactor(8))))
                {
                    this.LoadBackgroundImage();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void LoadImage(string ImageFileName)
        {
            try
            {
                this.BackGroundImage = new Bitmap(ImageFileName);
                this.GraphicsHandle = Graphics.FromImage(this.BackGroundImage);
                base.Size = new Size(this.BackGroundImage.Size.Width + GlobalParams.ScreenFactor(8), this.BackGroundImage.Size.Height + GlobalParams.ScreenFactor(8));
                base.RePainter();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void LoadImageBitmap(Bitmap ImageFileStream)
        {
            try
            {
                this.BackGroundImage = ImageFileStream;
                this.GraphicsHandle = Graphics.FromImage(this.BackGroundImage);
                base.Size = new Size(this.BackGroundImage.Size.Width + GlobalParams.ScreenFactor(8), this.BackGroundImage.Size.Height + GlobalParams.ScreenFactor(8));
                base.RePainter();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void LoadImageFromStream(Stream ImageFileStream)
        {
            try
            {
                this.BackGroundImage = new Bitmap(ImageFileStream);
                this.GraphicsHandle = Graphics.FromImage(this.BackGroundImage);
                base.Size = new Size(this.BackGroundImage.Size.Width + GlobalParams.ScreenFactor(8), this.BackGroundImage.Size.Height + GlobalParams.ScreenFactor(8));
                base.RePainter();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public override void OnMouseDown(MouseEventArgs e)
        {
            if (!this.Focused)
            {
                this.Focused = true;
                base.RePainter();
            }
            if ((e.X > (base.Size.Width - GlobalParams.ScreenFactor(0x10))) && (e.Y > (base.Size.Height - GlobalParams.ScreenFactor(0x10))))
            {
                this.sizeChanging = true;
                this.sizeStart = new Point(e.X, e.Y);
                this.sizeDirection = SizeChangeDirection.Both;
            }
            else if (((e.X > ((base.Size.Width / 2) - GlobalParams.ScreenFactor(12))) && (e.X < ((base.Size.Width / 2) - GlobalParams.ScreenFactor(4)))) && (e.Y > (base.Size.Height - GlobalParams.ScreenFactor(0x10))))
            {
                this.sizeChanging = true;
                this.sizeStart = new Point(e.X, e.Y);
                this.sizeDirection = SizeChangeDirection.Vertical;
            }
            else if (((e.X > (base.Size.Width - GlobalParams.ScreenFactor(0x10))) && (e.Y > ((base.Size.Height / 2) - GlobalParams.ScreenFactor(12)))) && (e.Y < ((base.Size.Height / 2) - GlobalParams.ScreenFactor(4))))
            {
                this.sizeChanging = true;
                this.sizeStart = new Point(e.X, e.Y);
                this.sizeDirection = SizeChangeDirection.Horizontal;
            }
            else if (!this.IsMathControlWasClicked)
            {
                this.IsMathControlWasClicked = true;
                this.LastMouseCoordinates.X = e.X;
                this.LastMouseCoordinates.Y = e.Y;
            }
        }

        public override void OnMouseMove(MouseEventArgs e)
        {
            if (!this.IsMathControlWasClicked)
            {
                if (this.sizeChanging)
                {
                    Size size = new Size();
                    if (this.sizeDirection == SizeChangeDirection.Both)
                    {
                        size = new Size(base.Size.Width + (e.X - this.sizeStart.X), base.Size.Height + (e.Y - this.sizeStart.Y));
                    }
                    else if (this.sizeDirection == SizeChangeDirection.Vertical)
                    {
                        size = new Size(base.Size.Width, base.Size.Height + (e.Y - this.sizeStart.Y));
                    }
                    else if (this.sizeDirection == SizeChangeDirection.Horizontal)
                    {
                        size = new Size(base.Size.Width + (e.X - this.sizeStart.X), base.Size.Height);
                    }
                    base.Size = new Size((size.Width >= 50) ? size.Width : 50, (size.Height >= 50) ? size.Height : 50);
                    this.sizeStart = new Point(e.X, e.Y);
                    base.RePainter();
                }
                else
                {
                    base.RePainter();
                }
            }
            else if (this.IsMathControlWasClicked)
            {
                LineToDraw draw = new LineToDraw();
                draw.StartX = this.LastMouseCoordinates.X;
                draw.StartY = this.LastMouseCoordinates.Y;
                draw.EndX = e.X;
                draw.EndY = e.Y;
                this.GraphicsHandle.DrawLine(this.SignaturePen, draw.StartX + 1, draw.StartY, draw.EndX + 1, draw.EndY);
                this.GraphicsHandle.DrawLine(this.SignaturePen, draw.StartX, draw.StartY + 1, draw.EndX, draw.EndY + 1);
                this.GraphicsHandle.DrawLine(this.SignaturePen, (int) (draw.StartX + 1), (int) (draw.StartY + 1), (int) (draw.EndX + 1), (int) (draw.EndY + 1));
                this.GraphicsHandle.DrawLine(this.SignaturePen, draw.StartX, draw.StartY, draw.EndX, draw.EndY);
                this.LastMouseCoordinates.X = draw.EndX;
                this.LastMouseCoordinates.Y = draw.EndY;
                base.RePainter();
            }
        }

        public override void OnMouseUp(MouseEventArgs e)
        {
            this.IsMathControlWasClicked = false;
        }

        public override void OnPaint(PaintEventArgs e)
        {
            this.LoadBackgroundImageIfInvalid();
            e.Graphics.DrawImage(this.BackGroundImage, e.ClipRectangle.X, e.ClipRectangle.Y);
            if (this.Focused)
            {
                e.Graphics.FillRectangle(base.brush[0], (e.ClipRectangle.X + base.Size.Width) - GlobalParams.ScreenFactor(13), (e.ClipRectangle.Y + base.Size.Height) - GlobalParams.ScreenFactor(13), GlobalParams.ScreenFactor(6), GlobalParams.ScreenFactor(6));
                e.Graphics.FillRectangle(base.brush[0], (e.ClipRectangle.X + base.Size.Width) - GlobalParams.ScreenFactor(13), (e.ClipRectangle.Y + (base.Size.Height / 2)) - GlobalParams.ScreenFactor(11), GlobalParams.ScreenFactor(6), GlobalParams.ScreenFactor(6));
                e.Graphics.FillRectangle(base.brush[0], (e.ClipRectangle.X + (base.Size.Width / 2)) - GlobalParams.ScreenFactor(11), (e.ClipRectangle.Y + base.Size.Height) - GlobalParams.ScreenFactor(13), GlobalParams.ScreenFactor(6), GlobalParams.ScreenFactor(6));
            }
        }

        public void SetPenColor(Color penColour)
        {
            this.SignaturePen = new Pen(penColour);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct LineToDraw
        {
            public int StartX;
            public int StartY;
            public int EndX;
            public int EndY;
        }

        private enum SizeChangeDirection
        {
            Horizontal,
            Vertical,
            Both
        }
    }
}

